package com.smartStatus.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartStatus.admin.service.SysRoleService;
import com.smartStatus.api.entity.SysRole;
import com.smartStatus.api.vo.TreeNodeVo;
import com.smartStatus.core.api.RedisKey;
import com.smartStatus.core.util.R;
import com.smartStatus.security.annoation.Ignore;
import com.smartStatus.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : SysRoleController
 * @Author : lby
 * @Date: 2020/12/29 10:15
 * @Description : 用户角色管理
 */
@RestController
@Api("用户角色管理")
@AllArgsConstructor
@Slf4j
@RequestMapping("/role")
//开启权限(默认false)
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final RedisTemplate redisTemplate;

    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("分页数据")
    @GetMapping("/page")
    public R<IPage> page(Page page, SysRole sysRole) {
        QueryWrapper<SysRole> query = Wrappers.query();
        if (StringUtils.isNotBlank(sysRole.getRoleName())) {
            query.like("role_name", sysRole.getRoleName());
        }
        query.eq("del_flag", 0);
        return R.ok(sysRoleService.page(page, query));
    }

    /**
     * 根据用户id查询角色
     */
    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("根据用户id查询角色")
    @GetMapping("/get-role/{id}")
//    @PreAuthorize("@pms.hasPermission('sys_dept_add')")
    public R getRoleByid(Integer id) {
        return R.ok(sysRoleService.getRoleInfo(id));
    }


    /**
     * 新增角色
     */
    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("新增角色")
    @PostMapping("/add")
    public R addRole(@RequestBody SysRole role) {
        return R.ok(sysRoleService.addRole(role), "修改成功");
    }

    /**
     * 编辑角色
     */
    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("编辑角色")
    @PostMapping("/edit")
    public R editRole(@RequestBody SysRole role) {
        return R.ok(sysRoleService.updateById(role), "编辑成功");
    }

    /**
     * 删除角色（批量）
     */
    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("删除角色（批量）")
    @PostMapping("/del")
    public R delRoles(@RequestBody Integer[] roleIds) {
        for (Integer id : roleIds) {
            sysRoleService.delRole(id);
        }
        return R.ok("删除成功");
    }

    /**
     * 添加角色与菜单关系
     */
    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("添加角色与菜单关系")
    @PostMapping("/add-role-menu")
    public R addRoleMenus(@RequestBody  Map<String, Object> map) {
        return sysRoleService.addRoelMenuTree(map);
    }

    /**
     * 获取角色对应的菜单
     */
    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @ApiOperation("获取角色对应的菜单")
    @GetMapping("/get-menu/by-id/{roleId}")
    public R getMenusById(@PathVariable("roleId") Integer roleId) {
        return sysRoleService.buildRoleTree(roleId);
    }


    /**
     * 角色列表数据
     */
//    @PreAuthorize("@pms.hasPermission('sys_role_view')")
    @Ignore(value = false)
    @ApiOperation("角色列表数据")
    @GetMapping("/list")
    public R getRoleLists() {
        return R.ok(sysRoleService.list());
    }
}
